<template>
    <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="收件人" required>
            <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" required>
            <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="详细地址" required>
            <el-row>
                <el-col :span="13">
                    <el-cascader
                            :options="options"
                            v-model="form.selectedOptions"
                            placeholder="请选择省/市/区"
                            @change="handleChange"
                            style="width: 100%"
                    ></el-cascader>
                </el-col>
                <el-col :span="11">
                    <el-input v-model="form.address"></el-input>
                </el-col>
            </el-row>
        </el-form-item>


        <div class="buttons">
            <el-button type="primary" @click="addresses">添加地址</el-button>
        </div>

    </el-form>
</template>

<script>
import {regionData, codeToText} from 'element-china-area-data'
import axios from "axios";

export default {
    data() {
        return {
            options: regionData,
            form: {
                name: '',
                phone: '',
                selectedOptions: [],
            },
            province: '',
            city: '',
            district: ''
        };
    },
    methods: {
        addresses() {
            axios({
                method: 'post',
                url: '/addresses',
                data: {
                    consignee: this.form.name,
                    consigneePhone: this.form.phone,
                    consigneeProvince: this.province,
                    consigneeCity: this.city,
                    consigneeDistrict: this.district,
                    consigneeDetialedAddress: this.form.address
                },
                headers: {
                    "token": window.sessionStorage.getItem("token")
                }
            }).then(res => {
                this.$message({
                    message: '添加成功',
                    type: 'success'
                });
            })

        },
        handleChange(value) {
            console.log(value)//打印区域码所对应的属性值即中文地址
            console.log(this.getCodeToText(null, value))
        },
        getCodeToText(codeStr, codeArray) {
            if (null === codeStr && null === codeArray) {
                return null;
            } else if (null === codeArray) {
                codeArray = codeStr.split(",");
            }

            let area = "";
            switch (codeArray.length) {
                case 1:
                    area += codeToText[codeArray[0]];
                    break;
                case 2:
                    area += codeToText[codeArray[0]] + "/" + codeToText[codeArray[1]];
                    break;
                case 3:
                    area +=
                        codeToText[codeArray[0]] +
                        "/" +
                        codeToText[codeArray[1]] +
                        "/" +
                        codeToText[codeArray[2]];
                    this.province = codeToText[codeArray[0]]
                    this.city = codeToText[codeArray[1]]
                    this.district = codeToText[codeArray[2]]
                    break;
                default:
                    break;
            }
            return area;
        }

    }
};
</script>

<style scoped>
.buttons {
    display: flex;
    justify-content: center;
    align-items: center;

}
</style>
